<?php


/**
 * @current article.php
 * @author   LiuYang <liu_yang@haibao.com>
 * @date 2017年2月14日 下午2:33:03
 */

namespace haibao\jiepai\business;

use haibao\jiepai\model\data\article\CmsArticles as CmsArticlesModelData;
use haibao\jiepai\data\Article as ArticleData;
use haibao\jiepai\data\CmsArticleTags as CmsArticleTagsData;
use haibao\jiepai\data\CmsArticleContent as CmsArticleContentData;
use haibao\jiepai\model\filter\article\CmsArticleContent as CmsArticleContentFilter;
use haibao\jiepai\model\filter\article\CmsArticleTags as CmsArticleTagsFilter;
use haibao\frame\data\query\Condition;
use haibao\classlibrary\jiepai\AppStaticConfig;
use haibao\jiepai\model\filter\article\CmsArticleCelebrity as CmsArticleCelebrityFilter;
use haibao\cms\business\Celebrity as CelebrityBusiness;
use haibao\cms\model\filter\Celebrity as CelebrityFilter;
use haibao\jiepai\data\Fashionista as FashionistaData;

class Article extends Base
{
    public static $articleHeadLine = array(
        CmsArticlesModelData::LINE_ALL => '所有头条',
        CmsArticlesModelData::LINE_ONE_IS => '序列1-内部',
        CmsArticlesModelData::LINE_TWO_IS => '序列2-转载',
    );

    public static $articleColumnList = array(
        '0' => '----------',
        1 => '街拍',
        '明星街拍' => '明星街拍',
        '博主街拍' => '博主街拍',
        '时尚圈街拍' => '时尚圈街拍',
        '00' => '----------',
        107 => '九点',
        '000' => '----------',
        101 => '一周看点',
        102 => '模范生',
        103 => '下周怎么穿',
        104 => '明星穿什么',
        105 => '种草',
        106 => '花边',
        108 => '街友评审团',
        '0000' => '----------',
        7 => '风尚',
        4 => '穿搭',
        21 => '人物',
        24 => '娱乐'
    );

    public static $allSpecialColumn = array(
        102 => '模范生',
        103 => '下周怎么穿',
        104 => '明星穿什么',
        105 => '种草',
        106 => '花边'
    );

    public static $allColumn = array(
        7 => '风尚',
        4 => '穿搭',
        21 => '人物',
        24 => '娱乐'
    );

    public static $imageSource = array(
        1 => array('TagId' => 89504, 'TagName' => '网络搜集'),
        2 => array('TagId' => 89505, 'TagName' => '博主官网'),
        3 => array('TagId' => 89720, 'TagName' => 'HAIBAO'),
        4 => array('TagId' => 89715, 'TagName' => '视觉中国'),
        5 => array('TagId' => 89687, 'TagName' => '品牌供图'),
        6 => array('TagId' => 89651, 'TagName' => '艺人经济公司'),
        8 => array('TagId' => 90486, 'TagName' => '摄影师/创作者本人')
    );

    public static $articleSubTiltleType = array(
        1 => '单品',
        2 => '品牌'
    );

    public static $dataItemName = array(
        'date' => '指定日期',
        'today' => '今日',
        'yesterday' => '昨日',
        'lastSevenDays' => '近7日',
        'thisWeek' => '本周',
        'lastThirtyDays' => '近30日',
        'thisMonth' => '本月',
    );

    public static function dealStatisticDate()
    {
        /* 本周起始时间 */
        $startWeek = date("Y-m-d  H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
        $endWeek = date("Y-m-d  H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y")));
        /* 本月起始时间 */
        $startMonth = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y")));
        $endMonth = date("Y-m-d  H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y")));

        /*指定日期;今日;昨日;近7日;本周;近30日;本月 */
        $dataItem = array(
            'date' => array('startDate' => '', 'endDate' => ''),
            'today' => array('startDate' => date("Y-m-d 00:00:00"), 'endDate' => date("Y-m-d 23:59:59")),
            'yesterday' => array('startDate' => date("Y-m-d 00:00:00", strtotime("-1 day")), 'endDate' => date("Y-m-d 23:59:59", strtotime("-1 day"))),
            'lastSevenDays' => array('endDate' => date("Y-m-d 00:00:00"), 'startDate' => date("Y-m-d 23:59:59", strtotime("-7 days"))),
            'thisWeek' => array('startDate' => $startWeek, 'endDate' => $endWeek),
            'lastThirtyDays' => array('endDate' => date("Y-m-d 00:00:00"), 'startDate' => date("Y-m-d 23:59:59", strtotime("-30 days"))),
            'thisMonth' => array('startDate' => $startMonth, 'endDate' => $endMonth),
        );
        return $dataItem;
    }

    public static function getArticleList($model)
    {
        $articleData = new ArticleData();
        return $articleData->getAll($model);
    }

    public static function delArticleById($id, $arrSet)
    {
        $articleData = new ArticleData();
        $articleData->updateById($id, $arrSet);
    }

    public static function getArticleListById($articleId)
    {
        if (!$articleId) {
            throw new BusinessException('Id不可为空!');
        }
        $articleData = new ArticleData();
        $data = $articleData->getOneById($articleId);

        $data->ArticleContentInfo = self::getArticleContentByArticleId($articleId);
        $data->TagInfoList = self::getArticleTagByArticleId($articleId);
        $data->celebrityList = self::getArticleCelebrityTagByArticleId($articleId);
        return $data;
    }

    public static function getArticleContentByArticleId($articleId)
    {
        $cmsArticleContentFilter = new CmsArticleContentFilter();
        $cmsArticleContentFilter->select(array(
            CmsArticleContentFilter::CONDITION_FIELD_NAME_ID,
            CmsArticleContentFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
            CmsArticleContentFilter::CONDITION_FIELD_NAME_CONTENT_INFO,
            CmsArticleContentFilter::CONDITION_FIELD_NAME_TYPE,
            CmsArticleContentFilter::CONDITION_FIELD_NAME_SHOW_INDEX,
            CmsArticleContentFilter::CONDITION_FIELD_NAME_IS_DETAIL,
            CmsArticleContentFilter::CONDITION_FIELD_NAME_IS_ORIGINAL
        ));
        $cmsArticleContentFilter->where(CmsArticleContentFilter::CONDITION_FIELD_NAME_ARTICLE_ID, Condition::CONDITION_EQUAL, $articleId);
        $cmsArticleContentData = new CmsArticleContentData();
        return $cmsArticleContentData->getAll($cmsArticleContentFilter);
    }

    public static function getArticleTagByArticleId($articleId)
    {
        $cmsArticleTagsFilter = new CmsArticleTagsFilter();
        $cmsTagsFilter = new \haibao\jiepai\model\filter\tag\CmsTags();
        $cmsArticleTagsFilter->select(array(
            CmsArticleTagsFilter::CONDITION_FIELD_NAME_ID,
            CmsArticleTagsFilter::CONDITION_FIELD_NAME_TAG_ID,
            CmsArticleTagsFilter::CONDITION_FIELD_NAME_ARTICLE_ID
        ));
        $cmsTagsFilter->select(array(
            \haibao\jiepai\model\filter\tag\CmsTags::CONDITION_FIELD_NAME_ID,
            \haibao\jiepai\model\filter\tag\CmsTags::CONDITION_FIELD_NAME_CATEGORY_ID
        ));
        $cmsArticleTagsFilter->order(CmsArticleTagsFilter::CONDITION_FIELD_NAME_ID);
        $cmsArticleTagsFilter->where(CmsArticleTagsFilter::CONDITION_FIELD_NAME_ARTICLE_ID, Condition::CONDITION_EQUAL, $articleId);
        $cmsTagsFilter->where(\haibao\jiepai\model\filter\tag\CmsTags::CONDITION_FIELD_NAME_CATEGORY_ID, Condition::CONDITION_IN, array(2, 3, 4, 5, 6, 7, 8, 9, 10));
        $cmsArticleTagsFilter->leftJoin($cmsTagsFilter, null, CmsArticleTagsFilter::CONDITION_FIELD_NAME_TAG_ID, \haibao\jiepai\model\filter\tag\CmsTags::CONDITION_FIELD_NAME_ID, \haibao\jiepai\model\filter\tag\CmsTags::CONDITION_FIELD_NAME_TAGS_RELATION, false);
        $cmsArticleTagsData = new CmsArticleTagsData();
        return $cmsArticleTagsData->getAll($cmsArticleTagsFilter);
    }

    public static function getArticleCelebrityTagByArticleId($articleId)
    {
        $cmsArticleCelebrityFilter = new CmsArticleCelebrityFilter();
        $cmsArticleCelebrityData = new \haibao\jiepai\data\CmsArticleCelebrity();
        $cmsArticleCelebrityFilter->select(array(
            CmsArticleCelebrityFilter::CONDITION_FIELD_NAME_ID,
            CmsArticleCelebrityFilter::CONDITION_FIELD_NAME_ARTICLEID,
            CmsArticleCelebrityFilter::CONDITION_FIELD_NAME_CELEBRITYID
        ));
        $cmsArticleCelebrityFilter->where(CmsArticleCelebrityFilter::CONDITION_FIELD_NAME_ARTICLEID, Condition::CONDITION_EQUAL, $articleId);
        $list = $cmsArticleCelebrityData->getAll($cmsArticleCelebrityFilter);
        $data = array();
        if ($list) {
            $celebrityIds = array();
            foreach ($list as $val) {
                array_push($celebrityIds, $val->CelebrityID);
            }
            $celebrityFilter = new CelebrityFilter;
            $celebrityFilter->select(array(
                CelebrityFilter::CONDITION_FIELD_NAME_ID,
                CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN,
                CelebrityFilter::CONDITION_FIELD_NAME_NAME_EN,
                CelebrityFilter::CONDITION_FIELD_NAME_IS_APP_USE
            ));
            $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_IS_APP_USE, Condition::CONDITION_EQUAL, \haibao\cms\model\data\Celebrity::ISAPPUSE_ENABLE);
            $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_IN, $celebrityIds);
            $dataList = CelebrityBusiness::getAll($celebrityFilter);
            foreach ($dataList as $key => $val) {
                $name = $val->NameEn;
                if ($val->NameCn) {
                    $name .= '(' . $val->NameCn . ')';
                }
                $data[$key]['id'] = $val->Id . '###' . $name;
                $data[$key]['text'] = $name;
            }
        }
        return json_encode($data);
    }

    /**
     * getArticleById
     * @param unknown $articleId array()
     * @throws BusinessException
     */
    public static function getArticleById($articleIds)
    {
        if (!$articleIds) {
            throw new BusinessException('Id不可为空!');
        }
        $articleData = new ArticleData();
        return $articleData->getArticleById($articleIds);
    }

    public static function getArticlePicById($articleIds)
    {
        if (!$articleIds) {
            throw new BusinessException('Id不可为空!');
        }
        $articleData = new ArticleData();
        return $articleData->getArticlePicById($articleIds);
    }

    public static function addArticle($model, $juryNum)
    {
        \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_APP_NEW_ARTICLE_MIXED_ADD);
        self::checkArticleModelData($model);
        $articleData = new ArticleData();
        $articleData->addArticleData($model);
        if ($model->Type == 108 && $juryNum > 0) {//生成文章时记录默认给每个人物一个喜欢一个不喜欢
            for ($i = 1; $i <= $juryNum; $i++) {
                $articleData->PointJuryArticle($model->Id, array('ContentIndex' => $i, 'IsLike' => 1), 1, 0);
                $articleData->PointJuryArticle($model->Id, array('ContentIndex' => $i, 'IsLike' => 0), 2, 0);
            }
        }
        \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_APP_NEW_ARTICLE_MIXED_ADD, $model->toArray(true, true));
    }

    public static function editArticle($model, $juryNum)
    {
        \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_APP_NEW_ARTICLE_MIXED_EDIT);
        self::checkArticleModelData($model);
        $articleData = new ArticleData();
        $articleData->updateArticleData($model);

        if ($model->Type == 108 && $juryNum > 0) {//生成文章时记录默认给每个人物一个喜欢一个不喜欢
            $articleData->delJuryActionList($model->Id, array(1, 2));  //删除生成文章时添加的默认喜欢和不喜欢
            for ($i = 1; $i <= $juryNum; $i++) {
                $articleData->PointJuryArticle($model->Id, array('ContentIndex' => $i, 'IsLike' => 1), 1, 0);
                $articleData->PointJuryArticle($model->Id, array('ContentIndex' => $i, 'IsLike' => 0), 2, 0);
            }
        }

        \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_APP_NEW_ARTICLE_MIXED_EDIT, $model->toArray(true, true));
    }

    private static function checkArticleModelData($model)
    {
        if (!$model) {
            throw new BusinessException('error');
        }
    }

    public static function getArticleStatisticList($conditions)
    {
        $articleData = new ArticleData();
        return $articleData->getArticleStatisticList($conditions);
    }

    /**
     * 文章统计
     * @param unknown $articleId (int/array())
     * @throws BusinessException
     * @return Ambigous <boolean, multitype:multitype:NULL  >
     */
    public static function getArticleClickNumsByArticleId($articleId)
    {
        if (!$articleId) {
            throw new BusinessException('ArticleId is empty!');
        }
        $articleData = new ArticleData();
        return $articleData->getArticleClickNumsByArticleId($articleId);
    }

    public static function getArticleStatisticDetailList($articleId, $page, $dataItem)
    {
        $articleData = new ArticleData();
        return $articleData->getArticleStatisticDetailList($articleId, $page, $dataItem);
    }

    public static function CheckJuryDate($publishTime, $endTime, $articleId)
    {
        $articleData = new ArticleData();
        return $articleData->CheckJuryDate($publishTime, $endTime, $articleId);
    }

    public static function CheckStatus($articleId)
    {
        if (!$articleId) {
            throw new BusinessException('ArticleId is empty!');
        }
        $articleData = new ArticleData();
        return $articleData->CheckStatus($articleId);
    }

    public static function getArticleDetail($articleId, $type = null)
    {
        $result['info'] = self::getArticle($articleId);
        if ($result['info']) {
            foreach ($result['info'] as $key => &$val) {
                $val['ArticleID'] = intval($val['ArticleID']);
                $val['Type'] = intval($val['Type']);
                $val['ColumnID'] = intval($val['ColumnID']);
            }
        }
        $result['contents'] = self::getArticleContents($articleId);
        $result['tags'] = self::getArticleTags($articleId);
        $result['otherArticles'] = self::getRecArticles($articleId, 1);
        $result['author'] = self::getArticleAuthor($articleId);
        $result['text'] = self::getArticleFirstTextContent($articleId, 2);
        return $result;
    }

    public static function getArticle($articleId)
    {
        $articleData = new ArticleData();
        return $articleData->getArticle($articleId);
    }

    /* type 6 为信息流题图 */
    public static function getArticleContents($articleId)
    {
        $articleData = new ArticleData();
        return $articleData->getArticleContents($articleId);
    }

    /* 文章作者信息 */
    public static function getArticleAuthor($articleId)
    {
        $articleData = new ArticleData();
        return $articleData->getArticleAuthor($articleId);
    }

    public static function getArticleTags($articleId)
    {
        $articleData = new ArticleData();
        return $articleData->getArticleTags($articleId);
    }

    /* 文章栏目信息 */
    public static function getArticleColumns($articleId, $columnId)
    {
        $articleData = new ArticleData();
        return $articleData->getArticleColumns($articleId, $columnId);
    }

    public static function getArticles($type, $cursor)
    {
        $articleData = new ArticleData();
        return $articleData->getArticles($type, $cursor);
    }

    public static function getColumnAndTagInfo($articleIds)
    {
        $articleData = new ArticleData();
        return $articleData->getColumnAndTagInfo($articleIds);
    }

    /**
     * 获取文章统计量
     * @param unknown $articleIds (int/array())
     * @param unknown $version
     */
    public static function getArticleStatisticTotal($articleIds)
    {
        $articleIds = is_array($articleIds) ? $articleIds : array($articleIds);
        $articleCache = new \haibao\jiepai\cache\Article();
        $articleStatisticArr = array();
        foreach ($articleIds as $k => $v) {
            $articleStatisticArr[$v] = $articleCache->getOneArticleStatistic($v);
        }
        $dataList = array();
        foreach ($articleIds as $key => $val) {
            if (isset($articleStatisticArr[$val]) && count($articleStatisticArr[$val]) > 0) {
                $k = 1;
                $v = 0;
                for ($i = 0; $i < count($articleStatisticArr[$val]) / 2; $i++) {
                    $dataList[$val][$articleStatisticArr[$val][$v]] = intval($articleStatisticArr[$val][$k]);
                    $k += 2;
                    $v += 2;
                }
            }
        }

        $articleData = new ArticleData();
        return $articleData->getArticleStatisticTotal($articleIds, $dataList);
    }

    /**
     *更新文章统计
     * @param unknown $articleIds (int/array)
     * @param unknown $name
     * @param number $num
     */
    public static function updateArticleStatistic($articleIds, $name, $num = 1)
    {
        $articleCache = new \haibao\jiepai\cache\Article();
        $articleIds = is_array($articleIds) ? $articleIds : array($articleIds);
        foreach ($articleIds as $k => $v) {
            $articleCache->updateArticleStatistic($v, $name, $num);
        }
    }

    public static function CheckCommentStatus($type, $typeId)
    {
        $articleData = new ArticleData();
        return $articleData->CheckCommentStatus($type, $typeId);
    }

    public static function CheckReportComment($type, $commentId, $userId)
    {
        $articleData = new ArticleData();
        return $articleData->CheckReportComment($type, $commentId, $userId);
    }

    public static function ReportComments($type, $commentId, $userId)
    {
        $articleData = new ArticleData();
        return $articleData->ReportComments($type, $commentId, $userId);
    }

    /**
     * 获取文章热门评论
     * @param $articeId
     * @param $version
     * @param int $limit
     * @return mixed
     */
    public static function getArticleHotComments($articeId, $limit = 2)
    {
        $data = new ArticleData();
        $commentList = $data->getArticleHotComments((int)$articeId, $limit);
        if ($commentList) {
            $commentList = self::resolveComment($commentList);
        }
        return $commentList;
    }

    public static function resolveComment($commentInfo)
    {
        $comments = array();
        foreach ($commentInfo as $comment) {

            $temp = array();
            $temp['CommentID'] = intval($comment['CommentID']);

            // TODO : 评论过滤敏感词
            $temp['Content'] = ($comment['AuditStatus'] == 1) ? $comment['Content'] : self::ReplaceSensitiveWords($comment['Content'], $comment['SensitiveWords']);
            $temp['CreateTime'] = $comment['CreateTime'];
            $temp['Scores'] = intval($comment['Scores']);
            $temp['Pictures'] = $comment['Pictures'];
            $temp['IsPraise'] = 0;
            $temp['User'] = array('UserID' => intval($comment['UserID']), 'NickName' => $comment['NickName'], 'Avatar' => \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_BBS) . '/uc_server/avatar.php?uid=' . $comment['UserID'] . '&size=middle');
            $temp['Pictures'] = $comment['Pictures'];
            array_push($comments, $temp);
        }
        return $comments;
    }

    /**
     * 获取街友评审团文章
     * @param $articleId
     */
    public static function getJuryArticleInfo($articleId)
    {

        $data = new ArticleData();
        return $data->getJuryArticleInfo((int)$articleId);
    }

    /**
     * 获取街友评审团文章文章投票数
     */
    public static function getJuryArticleVoteNum($articleId, $isComment = true)
    {

        $data = new ArticleData();
        return self::getJuryUserCount((int)$articleId) + self::getJuryCommentCount((int)$articleId);
    }

    public static function getJuryUserCount($articleId)
    {
        $data = new ArticleData();
        return $data->getJuryUserCount($articleId);
    }

    public static function getJuryCommentCount($articleId)
    {

        $data = new ArticleData();
        return $data->getJuryCommentCount($articleId);
    }

    /**
     * 更新评论统计
     * @param unknown $commentIDList (int/array)
     * @param unknown $name
     * @param unknown $num
     */
    public static function updateCommentStatistic($commentIDList, $name, $num)
    {
        $commentIDList = is_array($commentIDList) ? $commentIDList : array($commentIDList);
        $articleCache = new \haibao\jiepai\cache\Article();
        foreach ($commentIDList as $k => $v) {
            $articleCache->updateCommentStatistic($v, $name, $num);
        }
    }

    /**
     * 更新回复统计
     * @param unknown $commentIDList (int/array)
     * @param unknown $name
     * @param unknown $num
     */
    public static function updateCommentReplyStatistic($commentIDList, $name, $num)
    {
        $commentIDList = is_array($commentIDList) ? $commentIDList : array($commentIDList);
        $articleCache = new \haibao\jiepai\cache\Article();
        foreach ($commentIDList as $k => $v) {
            $articleCache->updateCommentReplyStatistic($v, $name, $num);
        }
    }

    public static function getRecArticles($topicId, $type = 2)
    {
        if (!$topicId) {
            throw new BusinessException('ID不为空');
        }
        $data = new ArticleData();
        return $data->getRecArticles((int)$topicId, $type);
    }

    public static function getArticleOtherDetailInfo($articleList, $articleIds)
    {
        $data = new ArticleData();
        return $data->getArticleOtherDetailInfo($articleList, $articleIds);
    }

    public static function getHomeHeadLineArticles()
    {
        $articleCache = new \haibao\jiepai\cache\Article();
        $dataList = $articleCache->getHomeHeadLineArticles();
        $data = new ArticleData();
        if ($dataList) {
            $dataList = $data::mergeDataAndImage($dataList, 'ContentInfo');
        }
        return $dataList;
    }

    public static function getColumnLists($version)
    {

        $data = new ArticleData();
        return $data->getColumnLists($version);
    }

    //获取是否已收藏功能
    public static function getArticleContentActions($fileId, $userId)
    {

        if (!$fileId || !$userId) {
            return 0;
        }

        $data = new ArticleData();
        return $data->getArticleContentActions($fileId, $userId);
    }

    public static function getArticleType($articleId)
    {

        $data = new ArticleData();
        return $data->getArticleType($articleId);
    }

    public static function getHomeAllSpecialArticles()
    {
        $data = new ArticleData();
        return $data->getHomeAllSpecialArticles();
    }

    public static function getLastJuryArticle($type = '')
    {
        $data = new ArticleData();
        return $data->getLastJuryArticle($type);
    }

    public static function getHeadLineArticles()
    {
        $data = new ArticleData();
        return $data->getHeadLineArticles();
    }

    public static function getArticleHomeColumnGuidance($type, $unInvoledArticleIDs)
    {
        $data = new ArticleData();
        return $data->getArticleHomeColumnGuidance($type, $unInvoledArticleIDs);
    }

    public static function addColumnDetailToArticle($articles)
    {

        if (!$articles) return array();
        $data = new ArticleData();
        $articleIds = array();
        foreach ($articles as $article) {
            array_push($articleIds, $article['ArticleID']);
        }

        $articlesColumns = $data->getColumnAndTagInfo($articleIds);

        $articleDetails = $data->getArticleOtherDetailInfo($articles, $articleIds);
        $list = array();
        foreach ($articles as $key => &$article) {
            if (isset($articlesColumns[$article['ArticleID']])) {
                $article['Column'] = $articlesColumns[$article['ArticleID']]['Columns'];
            }
            if (isset($articleDetails[$article['ArticleID']])) {
                $article = array_merge($article, $articleDetails[$article['ArticleID']]);
            }
        }
        return $articles;
    }

    //给文章列表加上评审团标识和评审团人数
    public static function addJuryArticlesInfo($list)
    {
        foreach ($list as $k => $v) {
            if ($v['Type'] == 108) {
                $list[$k]['IsVoteExists'] = 1;
                $list[$k]['VoteNum'] = self::getJuryArticleVoteNum($v['ArticleID']);
            }
        }
        return $list;
    }

    public static function getAdvertList()
    {
        $articleCache = new \haibao\jiepai\cache\Article();
        $articleCache->getAdvertList();
    }

    public static function getAuthorArticles($userId)
    {
        $data = new ArticleData();
        $dataList = $data->getAuthorArticles($userId);
        if ($dataList) {
            foreach ($dataList as $k => $v) {
                $dataList[$k]['IsPrePublish'] = 1;
            }
        }
        return $dataList;
    }

    public static function getPoseArticles($userId)
    {
        $data = new ArticleData();
        return $data->getLastJuryArticle();
    }

    public static function getJuryArticleIDList()
    {
        $data = new ArticleData();
        return $data->getJuryArticleIDList();
    }

    public static function getPrevJuryArticle($publishTime)
    {
        $data = new ArticleData();
        return $data->getPrevJuryArticle($publishTime);
    }

    public static function getIsJuryArticle($articleId, $userId, $visitorId)
    {

        if (!$userId && !$visitorId) return 0;
        $data = new ArticleData();
        return $data->getIsJuryArticle($articleId, $userId, $visitorId);

    }

    public static function getJuryArticleFashionistaList($articleId)
    {

        $data = new ArticleData();
        return $data->getJuryArticleFashionistaList($articleId);
    }

    public static function getRealJuryUserCount($articleId)
    {

        $data = new ArticleData();
        return $data->getRealJuryUserCount($articleId);
    }

    public static function getJuryHighest($articleId)
    {

        $data = new ArticleData();
        return $data->getJuryHighest($articleId);
    }

    public static function getArticleTitlePicture($articleId)
    {

        $data = new ArticleData();
        return $data->getArticleTitlePicture($articleId);
    }

    public static function getJuryFashionistaPoints($articleId, $k)
    {

        $data = new ArticleData();
        return $data->getJuryFashionistaPoints($articleId, $k);
    }

    public static function getWeekWearArticles($type, $region)
    {
        $articleCache = new \haibao\jiepai\cache\Article();
        return $articleCache->getWeekWearArticles($type, $region);
    }

    public static function PointJuryArticle($articleId, $item, $userId, $visitorId)
    {
        $data = new ArticleData();
        return $data->PointJuryArticle($articleId, $item, $userId, $visitorId);
    }

    public static function getBestCommentUsers($articleId)
    {
        $data = new ArticleData();
        return $data->getBestCommentUsers($articleId);
    }

    //获取评审团人物头像
    public static function getJuryArticleFashionistas($fashionistaId, $articleId, $userId, $visitorId, $contentIndex, $isGetJuryPoints = true)
    {
//         $oldFashionistaId = $fashionistaId;
//         $data = new FashionistaData();
//         $dataMap = $data->getCmsDataMap($fashionistaId, 2);

//         if(!$dataMap) return array();

//         $fashionistaId = $dataMap['mapId'];

        $selArr = array(
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_ID,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_PLACE,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_CN,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_EN,
        );
        $data = \haibao\jiepai\business\Fashionista::getCelebrityModels($fashionistaId, $selArr, true);
        $articleData = new ArticleData();
        $isLike = 0;
        if ($isGetJuryPoints) {
            $isLike = $articleData->getJuryArticleFashionistaIsLike($contentIndex, $articleId, $userId, $visitorId);
        }
        $dataList = array();
        if ($data) {
            $dataList['FashionistaID'] = $fashionistaId;
            $dataList['Name'] = $data->NameEn;
            $dataList['NameFr'] = $data->NameCn;
            $dataList['IsLike'] = $isLike > 0 ? 1 : 0;
            $dataList['Avatar'] = $data->ImageSrc;
        }
        return $dataList;
    }

    public static function getJuryArticleFashionIsLike($contentIndex, $articleId, $userId, $visitorId)
    {
        $articleData = new ArticleData();
        $count = $articleData->getJuryArticleFashionistaIsLike($contentIndex, $articleId, $userId, $visitorId);
        return intval($count) > 0;
    }

    public static function getJuryArticleUnLikePeopleCount($articleId, $isSpecial = false)
    {
        $data = new ArticleData();
        return $data->getJuryArticleUnLikePeopleCount($articleId, $isSpecial);
    }

    /* 获取人物信息 */
    public static function getJuryFashionistaInfo($fashionistaId)
    {
        $selArr = array(
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_ID,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_CN,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_EN,
        );
        $data = \haibao\jiepai\business\Fashionista::getCelebrityModels($fashionistaId, $selArr, true, \haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_ICON);
        $dataList = array();
        if ($data) {
            $dataList['FashionistaID'] = $fashionistaId;
            $dataList['Name'] = $data->NameEn;
            $dataList['NameFr'] = $data->NameCn;
        }
        return $dataList;
    }

    public static function getJuryComments($articleId, $contentIndex, $IsBest)
    {
        $data = new ArticleData();
        return $data->getJuryComments($articleId, $contentIndex, $IsBest);
    }


    public static function getLastestNineClockArticle()
    {
        $data = new ArticleData();
        return $data->getLastestNineClockArticle();
    }

    public static function getPictureNum($articleId, $type = null)
    {
        $data = new ArticleData();
        return $data->getPictureNum($articleId, $type);
    }

    public static function getNineClockArticle($articleId)
    {
        $data = new ArticleData();
        $nineArticle = $data->getArticleInfoById((int)$articleId);
        $content = $data->getNineClockArticleContent((int)$articleId);
        $juryArticle = self::getLastJuryArticle('nineClock');

        return array($nineArticle, $content, $juryArticle);
    }

    //获取正文第一段文字字段
    public static function getArticleFirstTextContent($articleId, $type)
    {
        $data = new ArticleData();
        return $data->getArticleFirstTextContent((int)$articleId, $type);

    }

    public static function getArticleHaveVideo($articleId)
    {

        $data = new ArticleData();
        return $data->getArticleHaveVideo($articleId);
    }

    public static function getJuryDuringComments($articleId, $contentIndex)
    {

        $data = new ArticleData();
        return $data->getJuryDuringComments($articleId, $contentIndex);
    }

    public static function setHotCommentStatistic($results, $userId)
    {

        $data = new ArticleData();
        return $data->setHotCommentStatistic($results, $userId);
    }

    public static function gtLastDuringJuryArticle()
    {
        $data = new ArticleData();
        return $data->gtLastDuringJuryArticle();
    }

    //获取优选列表文章信息
    public static function getBestUserListArticle($articleId)
    {


        $highestArticle = self::getJuryHighest($articleId);
        $fashions = self::getJuryArticleFashionistaList($articleId);

        $fashionIndex = (isset($highestArticle) && isset($highestArticle[0])) ? $highestArticle[0]['ContentIndex'] - 1 : 0;
        $fashionistaInfo = isset($fashions[$fashionIndex]) ? $fashions[$fashionIndex] : array();


        $juryArticleInfo = self::getJuryArticleInfo($articleId);
        return array($juryArticleInfo, $fashionistaInfo);

    }

    //根据ArticleID获取上一期和下一期街友评审团ID
    public static function getJuryCursor($articleId)
    {

        $data = new ArticleData();
        return $data->getJuryCursor($articleId);
    }

    //获取街友评审团总个数
    public static function getJuryArticleTotalNum()
    {

        $data = new ArticleData();
        return $data->getJuryArticleTotalNum();
    }

    //获取评审团文章属于第几期
    public static function getJuryArticleIndex($articleId)
    {

        $data = new ArticleData();
        return $data->getJuryArticleIndex($articleId);
    }

    public static function getArticleAuthorInfo($articleId, $userId)
    {

        $data = new ArticleData();
        return $data->getArticleAuthorInfo($articleId, $userId);
    }

    // 获取评论图片
    public static function getCommentPictures($typeId, $type)
    {

        $data = new ArticleData();
        return $data->getCommentPictures((int)$typeId, (int)$type);
    }

    public static function setArticleStatistic($otherArticles)
    {
        $statistic = self::getOldStatistic($otherArticles);
        $data = array();
        foreach ($otherArticles as $key => $model) {
            $temp[$model['ArticleID']] = self::getArticleStatistic($model['ArticleID']);
            if (isset($temp[$model['ArticleID']]) && count($temp[$model['ArticleID']]) > 0) {
                $k = 1;
                $v = 0;
                for ($i = 0; $i <= count($temp[$model['ArticleID']]) / 2; $i += 2) {
                    $data[$model['ArticleID']][$temp[$model['ArticleID']][$v]] = intval($temp[$model['ArticleID']][$k]);
                    $k += 2;
                    $v += 2;
                }
            }
            if (isset($statistic[$model['ArticleID']]['OldJiepaiStatistic']) && $statistic[$model['ArticleID']]['OldJiepaiStatistic']) {
                $temp = json_decode($statistic[$model['ArticleID']]['OldJiepaiStatistic']);
                $data[$model['ArticleID']]['BrowseNum'] = isset($data[$model['ArticleID']]['BrowseNum']) ? $data[$model['ArticleID']]['BrowseNum'] : 0 + ($temp ? intval($temp['BrowseNum']) : 0);
                $data[$model['ArticleID']]['PraiseNum'] = isset($data[$model['ArticleID']]['PraiseNum']) ? $data[$model['ArticleID']]['PraiseNum'] : 0 + ($temp ? intval($temp['PraiseNum']) : 0);
            } else {
                $data[$model['ArticleID']]['BrowseNum'] = isset($data[$model['ArticleID']]['BrowseNum']) ? intval($data[$model['ArticleID']]['BrowseNum']) : 0;
                $data[$model['ArticleID']]['PraiseNum'] = isset($data[$model['ArticleID']]['PraiseNum']) ? intval($data[$model['ArticleID']]['PraiseNum']) : 0;
            }
            $data[$model['ArticleID']]['CollectNum'] = isset($data[$model['ArticleID']]['CollectNum']) ? intval($data[$model['ArticleID']]['CollectNum']) : 0;
            $data[$model['ArticleID']]['AddBrowseNum'] = isset($data[$model['ArticleID']]['AddBrowseNum']) ? intval($data[$model['ArticleID']]['AddBrowseNum']) : 0;

            $data[$model['ArticleID']]['AddPraiseNum'] = isset($data[$model['ArticleID']]['AddPraiseNum']) ? intval($data[$model['ArticleID']]['AddPraiseNum']) : 0;
            $data[$model['ArticleID']]['IsCommentHot'] = isset($data[$model['ArticleID']]['IsCommentHot']) ? intval($data[$model['ArticleID']]['IsCommentHot']) : 0;
            $otherArticles[$key]['Statistic'] = isset($data[$model['ArticleID']]) ? $data[$model['ArticleID']] : array('BrowseNum' => 0, 'AddBrowseNum' => 0, 'AddPraiseNum' => 0, 'CollectNum' => 0, 'PraiseNum' => 0, 'IsCommentHot' => 0);
            if (empty($model['PictureNum'])) {
                $otherArticles[$key]['PictureNum'] = self::getPictureNum($model['ArticleID'], $model['Type']);
            }
        }
        return $otherArticles;
    }

    public static function getArticleStatistic($articleId)
    {
        $articleCache = new \haibao\jiepai\cache\Article();
        return $articleCache->getOneArticleStatistic($articleId);
    }

    public static function getOldStatistic($results)
    {
        $findOldArticleStatistic = "";
        $articleIds = $dataList = array();
        if ($results) {
            foreach ($results as $k => $v) {
                array_push($articleIds, $v['ArticleID']);
            }
        }
        if (count($articleIds) > 0) {
            $data = new ArticleData();
            $dataList = $data->getOldStatistic($articleIds);
        }
        return $dataList;
    }

    public static function getArticleUserActions($articleId, $userId)
    {
        $isPraise = \haibao\jiepai\business\User::getUserActions(1, $articleId, $userId);
        $isCollect = \haibao\jiepai\business\User::getUserActions(2, $articleId, $userId);
        $results['IsPraise'] = count($isPraise) ? count($isPraise) : 0;
        $results['IsCollect'] = count($isCollect) ? count($isCollect) : 0;
        return $results;
    }

    public static function getComments($option, $articleId)
    {
        $data = new ArticleData();
        $res = $data->getComments($option, $articleId);
        if ($res['results']) {
//     		$res['results'] = $data->mergeDataAndImage($res['results'],'Pictures');
            $res['results'] = self::resolveComment($res['results']);
        } else {
            $res['results'] = $res['results'];
        }
        return $res;
    }

    public static function getCommentReply($commentIds)
    {
        $data = new ArticleData();
        $commentReply = $data->getCommentReply($commentIds);
        return $commentReply;
    }

    public static function getCommentUserActions($commentIds, $userId)
    {
        $data = new \haibao\jiepai\data\User();
        return $data->getUserActions(3, $commentIds, $userId);
    }

    public static function getReplyIsPraise($replyIds, $userId)
    {
        $data = new \haibao\jiepai\data\User();
        return $data->getUserActions(5, $replyIds, $userId);
    }

    public static function getCommentReplyStatistic($replyId)
    {
        $data = new \haibao\jiepai\cache\Article();
        $result = $data->getCommentReplyStatistic($replyId);
        $dataList = array();
        if ($result && count($result) > 0) {
            $k = 1;
            $v = 0;
            for ($i = 0; $i <= count($result) / 2; $i += 2) {
                $dataList[$result[$v]] = intval($result[$k]);
                $k += 2;
                $v += 2;
            }
        }
        return $dataList;
    }

    public static function getCommentStatistic($commentId)
    {
        $data = new \haibao\jiepai\cache\Article();
        $result = $data->getCommentStatistic($commentId);
        $dataList = array();
        if ($result && count($result) > 0) {
            $k = 1;
            $v = 0;
            for ($i = 0; $i <= count($result) / 2; $i += 2) {
                $dataList[$result[$v]] = intval($result[$k]);
                $k += 2;
                $v += 2;
            }
        }

        return $dataList;
    }

    //通过评论ID 获取文章题图
    public static function getArticleByComment($commentId)
    {

        $data = new ArticleData();
        return $data->getArticleByComment((int)$commentId);
    }

    //获取专栏回顾文章列表
    public static function getSpecialReviews($articleId, $type, $publishtime)
    {

        $data = new ArticleData();
        return $data->getSpecialReviews($articleId, $type, $publishtime);
    }

    public static function checkCommentValidStatus($type, $obj)
    {
        $data = new \haibao\jiepai\data\Article();
        return $data->checkCommentValidStatus($type, $obj);
    }

    /* 获取文章评论和回复总数 */
    public static function getCommentTotalCount($articleId)
    {
        $data = new \haibao\jiepai\data\Article();
        return $data->getCommentTotalCount($articleId);
    }

    public static function getHomeAllArticles($cursor)
    {
        $list = self::getArticles(1, $cursor);
        $results = array();
        if ($list) {
            $articleIds = array();
            foreach ($list as $k => $v) {
                array_push($articleIds, $v['ArticleID']);
            }
            $results['ColumnInfo'] = self::getColumnAndTagInfo($articleIds);
            $results['Others'] = $results['Details'] = array();
            $results['Details'] = self::getArticleOtherDetailInfo($list, $articleIds);
            foreach ($list as $key => $val) {
                $results['Others'][$val['ArticleID']] = self::getArticleOtherInfo($val['ArticleID']);
            }
            foreach ($list as $key => $item) {
                $list[$key] = array_merge($item, $results['Details'][$item['ArticleID']]);
                $list[$key]['Comments'] = $results['Others'][$item['ArticleID']];
                $list[$key]['Column'] = isset($results['ColumnInfo'][$item['ArticleID']]) ? $results['ColumnInfo'][$item['ArticleID']]['Columns'] : array();
                $list[$key]['Column']['ClickType'] = AppStaticConfig::getConfig(AppStaticConfig::APP_COLUMN_HOME_ALL);
            }
        }
        return array('list' => $list);
    }

    public static function GetHomeArticles($query)
    {
        $list = self::getArticles($query, 1, '');
        $results = array();
        if ($list) {
            $articleIds = array();
            foreach ($list as $k => $v) {
                array_push($articleIds, $v['ArticleID']);
                $results['Comments'][$v['ArticleID']] = self::getArticleHotComments($v['ArticleID']);
            }
            $data = new ArticleData();
            $results['Goods'] = $data->getArticleGoods($articleIds);

            foreach ($list as $k => $v) {
                $list[$k]['Comments'] = $results['Comments'][$v['ArticleID']]['Comments'];
                $list[$k]['Goods'] = $results['Goods'][$v['ArticleID']];
            }
        }
        return array('list' => $list);
    }

    public static function getArticleOtherInfo($articleId)
    {
        return self::getArticleHotComments($articleId);
    }

    public static function getArticleInfo($articleId, $contentType = 6)
    {

        $data = new \haibao\jiepai\data\Article();
        return $data->getArticleInfo($articleId, $contentType);
    }

    public static function getMixArticleRecommend($articleId, $recommendArticleId)
    {
        $data = new \haibao\jiepai\data\Article();
        return $data->getMixArticleRecommend($articleId, $recommendArticleId);

    }

    public static function getImageInfo($imageId)
    {
        $imageFilter = new \haibao\cms\model\filter\ImageLibrary();
        $imageFilter->select(array(
            $imageFilter::CONDITION_FIELD_NAME_ID,
            $imageFilter::CONDITION_FIELD_NAME_WIDTH,
            $imageFilter::CONDITION_FIELD_NAME_HEIGHT,
        ));

        $imageFilter->where(\haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $imageId);
        $data = new \haibao\cms\data\ImageLibrary();
        $dataInfo = $data->getOne($imageFilter);

        if ($dataInfo) {
            return array($dataInfo->Width, $dataInfo->Height);
        }
        return array();
    }
}